
package com.sun.showcase.biz.testcase.service.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.showcase.biz.testcase.dao.CaseProjectDao;
import com.sun.showcase.biz.testcase.service.CaseProjectService;
import com.sun.showcase.client.testcase.domain.CaseProject;
import com.sun.showcase.client.testcase.query.CaseProjectQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Pager;
import com.sun.showcase.utils.LoginContextHolder;
import com.sun.showcase.utils.StringConstant;
@Service("caseProjectService")
@Transactional
public class CaseProjectServiceImpl implements CaseProjectService{
	@Autowired
	private CaseProjectDao caseProjectDao;
	
	public void setCaseProjectDao(CaseProjectDao dao) {
		this.caseProjectDao = dao;
	}

	public DataGrid datagrid(CaseProjectQuery caseProjectQuery) {
		DataGrid j = new DataGrid();
		Pager<CaseProject> pager  = find(caseProjectQuery);
		j.setRows(getQuerysFromEntitys(pager.getRecords()));
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<CaseProjectQuery> getQuerysFromEntitys(List<CaseProject> caseProjects) {
		List<CaseProjectQuery> caseProjectQuerys = new ArrayList<CaseProjectQuery>();
		if (caseProjects != null && caseProjects.size() > 0) {
			for (CaseProject tb : caseProjects) {
				CaseProjectQuery b = new CaseProjectQuery();
				BeanUtils.copyProperties(tb, b);
				caseProjectQuerys.add(b);
			}
		}
		return caseProjectQuerys;
	}

	private Pager<CaseProject> find(CaseProjectQuery caseProjectQuery) {
		Page<CaseProject> page = PageHelper.startPage(caseProjectQuery.getPage().intValue(), caseProjectQuery.getRows().intValue());
		List<CaseProject> list = caseProjectDao.findList(caseProjectQuery);
		Pager<CaseProject> pager = new Pager<CaseProject>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	public CaseProject add(CaseProjectQuery caseProjectQuery) {
		CaseProject t = new CaseProject();
		BeanUtils.copyProperties(caseProjectQuery, t);
		t.setActiveFlag(StringConstant.ONE);
		t.setCreateBy(LoginContextHolder.get().getUserId());
		t.setCreateByName(LoginContextHolder.get().getUserName());
		t.setCreateDate(new Date());
	    t.setModifiedBy(LoginContextHolder.get().getUserId());
	    t.setModifiedByName(LoginContextHolder.get().getUserName());
	    t.setModifiedDate(new Date());
		caseProjectDao.save(t);
		caseProjectQuery.setId(t.getId());
		return t;
		
	}

	public void update(CaseProjectQuery caseProjectQuery) {
		CaseProject t = caseProjectDao.getById(caseProjectQuery.getId());
	    if(t != null) {
	    	BeanUtils.copyProperties(caseProjectQuery, t);
		    t.setModifiedBy(LoginContextHolder.get().getUserId());
		    t.setModifiedByName(LoginContextHolder.get().getUserName());
		    t.setModifiedDate(new Date());
	    	caseProjectDao.update(t);
		}
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(CaseProjectQuery caseProjectQuery) {
	    caseProjectDao.updatePart(caseProjectQuery);
	}

	public void delete(java.lang.Long[] ids) {
		caseProjectDao.deleteAc(ids);
	}
	
	//逻辑删除 更新状态位
	public void deletePt(java.lang.Long[] ids) {
		caseProjectDao.deletePt(ids);
	}

	public CaseProject get(CaseProjectQuery caseProjectQuery) {
		return caseProjectDao.getById(new Long(caseProjectQuery.getId()));
	}

	public CaseProject get(Long id) {
		return caseProjectDao.getById(id);
	}

	
	public List<CaseProjectQuery> listAll(CaseProjectQuery caseProjectQuery) {
	    List<CaseProject> list = caseProjectDao.findList(caseProjectQuery);
		List<CaseProjectQuery> listQuery =getQuerysFromEntitys(list) ;
		return listQuery;
	}
	
	
}
